/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.webanovare.bb;

import com.webanovare.bb.AuthBB;
import com.webanovare.bb.BoardBB;
import com.webanovare.ruzzle.mgr.GameManager;
import com.webanovare.ruzzle.mgr.impl.IGame;
import com.webanovare.ruzzle.misc.Constants;
import com.webanovare.ruzzle.misc.Player;
import com.webanovare.ruzzle.misc.TimeSetting;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.icefaces.application.PushRenderer;

@ManagedBean(name = "gamersBean")
@ViewScoped
public class GameBB implements Serializable, PropertyChangeListener {

    private static final String PUSH_GROUP = "everyone";
    private String sessionId;
    private GameManager gm = GameManager.getInstance();
    private int gameId;
    private String owner;
    private String opponent;
    @ManagedProperty(value = "#{board}")
    BoardBB genderBender;
    @ManagedProperty(value = "#{auth}")
    AuthBB authBean;
    private String language;
    private int size;
    private int minutes;
    private int seconds;
    
        public void setSize(int size) {
        this.size = size;
    }

    public void setMinutes(int minutes) {
        this.minutes = minutes;
    }

    public void setSeconds(int seconds) {
        this.seconds = seconds;
    }
    public int getSize() {
        return size;
    }

    public int getMinutes() {
        return minutes;
    }

    public int getSeconds() {
        return seconds;
    }
    public void setLanguage(String language) {
        System.out.println("lang: " + language);
        this.language = language;
    }
    public String getLanguage() {
        System.out.println("get lang: " + language);
        return this.language;
    }
    public void setGenderBender(BoardBB b) {
        this.genderBender = b;
    }

    public BoardBB getGenderBender() {
        return this.genderBender;
    }
    
    public void setAuthBean(AuthBB b) {
        this.authBean = b;
    }
    
    public AuthBB getAuthBean() {
        return this.authBean;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public void setOpponent(String opponent) {
        this.opponent = opponent;
    }

    public int getGid() {
        return this.gameId;
    }

    public String getOwner() {
        return this.owner;
    }

    public String getOpponent() {
        return this.opponent;
    }

    public GameBB() {
        PushRenderer.addCurrentSession(PUSH_GROUP);
        FacesContext fcontext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fcontext.getExternalContext().getSession(false);
        this.sessionId = session.getId();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        switch (evt.getPropertyName()) {
            case Constants.PROP_GAME_CREATED:
                this.send();
                break;
        }
    }

    public List<IGame> getGames() {
        Logger.getAnonymousLogger().log(Level.INFO, this.authBean.getLoggedInAs() + " försöker hämta spel och " + 
                (this.gm.getGames().isEmpty() ? "är inte tom" : "är tom"));
        return this.gm.getGames();
    }

    public void send() {
        PushRenderer.render(PUSH_GROUP);
    }

    public void createGame() {
        int gid;
        
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        
        String owner = params.get("owner");
        int size = this.size;
        
        int minutes = this.minutes;
        int seconds = this.seconds;
        
        if ((gid = this.gm.createGame(new Player(owner), size, new TimeSetting(seconds, minutes), this.language)) != -1) {
            this.send();

            this.gameId = gid;

            genderBender.setPushGroup(gid + "");
            this.genderBender.setGame(this.gm.getGame(gid));

            PushRenderer.addCurrentSession(gid + "");
        }
    }
    
    public boolean isPlaying() {
        IGame g = GameManager.getInstance().getGame(new Player(this.authBean.getLoggedInAs()));
        if(g == null) {
            return false;
        }
        return true;
    }

    public void challenge() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        int gameId = Integer.parseInt(params.get("gid"));
        String challenger = params.get("challenger");

        IGame g = gm.getGame(gameId);

        if (!challenger.equals(g.getOwner().toString())) {
            g.challenge(new Player(challenger));
            g.accept();

            genderBender.setPushGroup(gameId + "");
            this.genderBender.setGame(this.gm.getGame(gameId));
            //this.genderBender.setActive(true);

            PushRenderer.addCurrentSession(gameId + "");
            PushRenderer.render(gameId + "");
        }
    }
}
